package org.dreamfly.netshop.actions.member;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.dreamfly.netshop.entity.Address;
import org.dreamfly.netshop.entity.Area;
import org.dreamfly.netshop.entity.GoodsInfo;
import org.dreamfly.netshop.entity.Member;
import org.dreamfly.netshop.entity.OrderInfo;
import org.dreamfly.netshop.entity.PayType;
import org.dreamfly.netshop.entity.ShipType;
import org.dreamfly.netshop.entity.ShopCart;
import org.dreamfly.netshop.manage.AddressManager;
import org.dreamfly.netshop.manage.BuyInfoManager;
import org.dreamfly.netshop.manage.GoodsInfoManager;
import org.dreamfly.netshop.manage.OrderInfoManager;
import org.dreamfly.netshop.manage.ShopCartManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 用户管理Action. 使用Struts2 convention-plugin annotation定义Action参数.
 * 
 * @author calvin
 */
@Namespace(value = "/member/shopcart")
public class ShopAction extends ActionSupport {

//    @Autowired
//    private MemberManager memberManager;

    @Autowired
    private ShopCartManager shopCartManager;

    @Autowired
    private AddressManager addressManager;

    @Autowired
    private OrderInfoManager orderInfoManager;

    @Autowired
    private BuyInfoManager buyInfoManager;

    @Autowired
    private GoodsInfoManager goodsInfoManager;

    private Member member;

    private Address address;

    private Long id;

    private List<PayType> ptList;

    private List<ShipType> stList;

    private List<Area> aList;

    private List<ShopCart> scList = new ArrayList<ShopCart>();

    private Page<Address> addresses = new Page<Address>(20);

    private Page<ShopCart> shopCart = new Page<ShopCart>(20);

    public Page<ShopCart> getShopCart() {
        return shopCart;
    }

    public List<ShopCart> getScList() {
        return scList;
    }

    public void setScList(List<ShopCart> scList) {
        this.scList = scList;
    }

    public List<PayType> getPtList() {
        return ptList;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Page<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(Page<Address> addresses) {
        this.addresses = addresses;
    }

    public void setShopCart(Page<ShopCart> shopCart) {
        this.shopCart = shopCart;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<ShipType> getStList() {
        return stList;
    }

    public List<Area> getAList() {
        return aList;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public String execute() {
        // ActionContext ctx = ActionContext.getContext();
        // HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        // HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
        // classManager.delete((long) 21);
        return SUCCESS;
    }

    public String getShopCartList() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        // shopCart = shopCartManager.getShopCartListByMemberId(member.getId(), shopCart);
        scList = shopCartManager.getShopCartListByMemberId(member.getId());
        // if (shopCart.getResult().size() == 0 && shopCart.getPageNo() != 1) {
        // shopCart.setPageNo(shopCart.getPrePage());
        // shopCart = shopCartManager.getShopCartListByMemberId(member.getId(), shopCart);
        // }
        return "list";
    }

    public String deleteAllShopCart() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        shopCartManager.deleteAllByMemberID(member.getId());
        try {
            ServletActionContext.getResponse().sendRedirect(
                    ServletActionContext.getRequest().getContextPath() + "/member/shopcart/shop!getShopCartList");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return NONE;
    }

    public String deleteShopCartByID() {
        shopCartManager.delete(id);
        getShopCartList();
        return "list";
    }

    /**
     * 选择支付银行
     * 
     * @return
     */
    public String pay() {
        OrderInfo orderInfo = orderInfoManager.get(id);
        orderInfo.setIsPayment(true);
        return "pay";
    }

    /**
     * 确认收货
     * 
     * @return
     */
    public String getGoods() {
        OrderInfo orderInfo = orderInfoManager.get(id);
        orderInfo.setIsConfirm(true);
        return "over";
    }

    public String choseAddress() {
        addresses.setOrderBy("id");
        addresses.setOrder(Page.DESC);
        addresses = addressManager.getAll(addresses);
        getShopCartList();
        return "address";
    }

    public String buyAll() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        Double sum = shopCartManager.getPriceFromShopCartByMemberID(member.getId());
        // addresses = addressManager.getAll(addresses);
        if (id == null || "".equals(id)) {
            address.setMember(member);
            addressManager.save(address);
        } else
            address = addressManager.get(id);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setGoodsFee(sum);
        orderInfo.setShipFee(10.00);
        orderInfo.setTotalPrice(sum + 10.00);
        orderInfo.setOrderDate(new Date());
        orderInfo.setIsConfirm(false);
        orderInfo.setIsConsignment(false);
        orderInfo.setIsPayment(false);
        orderInfo.setIsPigeonhole(false);
        orderInfo.setMember(member);
        orderInfo.setAddress(address);
        orderInfoManager.save(orderInfo);
        List<ShopCart> spList = shopCartManager.getShopCartListByMemberId(member.getId());
        buyInfoManager.saveAllShopCart(spList, orderInfo);
        // TODO DO NOT NEED COMMENT
        // shopCartManager.deleteAllByMemberID(member.getId());
        request.setAttribute("sum", sum);
        request.setAttribute("orderInfo", orderInfo);
        return "checkout";
    }

    public String addAddress() {
        addresses.setOrderBy("id");
        addresses.setOrder(Page.DESC);
        HttpSession session = ServletActionContext.getRequest().getSession();
        Member member = (Member) session.getAttribute("member");
        address.setMember(member);
        addressManager.save(address);
        addresses = addressManager.getAll(addresses);
        getShopCartList();
        return "address";
    }

    public String addToShopCart() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = ServletActionContext.getRequest().getSession();
        String num = request.getParameter("num");
        if (num == null || "".equals(num))
            num = "1";
        Member member = (Member) session.getAttribute("member");
        ShopCart cart = new ShopCart();
        cart.setMember(member);
        GoodsInfo goodsInfo = goodsInfoManager.get(id);
        cart.setNum(Integer.valueOf(num));
        cart.setSumPrice(Integer.valueOf(num) * goodsInfo.getMemberPrice());
        cart.setGoodsInfo(goodsInfo);
        cart.setMember(member);
        cart.setGoodsWeight(goodsInfo.getGoodsWeight());
        shopCartManager.addShopCart(cart);
        getShopCartList();
        return "list";
    }

    public String payOrder() {
        HttpServletRequest request = ServletActionContext.getRequest();
        OrderInfo orderInfo = orderInfoManager.get(id);
        request.setAttribute("orderInfo", orderInfo);
        return "checkout";
    }

    /**
     * 取消订单
     * 
     * @return
     */
    public String cancelOrder() {
        HttpServletRequest request = ServletActionContext.getRequest();
        OrderInfo orderInfo = orderInfoManager.get(id);
        request.setAttribute("orderInfo", orderInfo);
        return "checkout";
    }

    /**
     * 付款
     * 
     * @return
     */
    public String checkout() {
        OrderInfo orderInfo = orderInfoManager.get(id);
        orderInfo.setIsPayment(true);
        try {
            ServletActionContext.getResponse().sendRedirect(
                    ServletActionContext.getRequest().getContextPath() + "/member/shopcart/shop!getShopCartList");
        } catch (IOException e) {
            e.printStackTrace();
        }
        orderInfoManager.save(orderInfo);
        return NONE;
    }

}
